/*
 * Copyright (c) 2016 咖枯 <kaku201313@163.com | 3772304@qq.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.future.mayi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.future.mayi.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * @author 图片
 * @version 1.0 2016/5/19
 */
public class ApplyImgAdapter extends BaseRecyclerViewAdapter<String> {

    private int imgHeight;
    private int margin;
    private boolean openSelect = false;
    private int maxSelectNum = 3;

    public ApplyImgAdapter(Context context, List<String> mList) {
        super(context, mList);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        final View view;
        view = getView(parent, R.layout.layout_list_img_item);
        if (imgHeight != 0) {
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(imgHeight + margin, imgHeight + margin);
            view.setLayoutParams(layoutParams);
        }
        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            setItemValues((ItemViewHolder) holder, position);
        }
    }

    private void setItemValues(final ItemViewHolder holder, final int position) {
        if (mOnItemClickListener != null) {
            holder.ivImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.OnItem(v, position);
                }
            });

            if (openSelect)
                holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.OnItem(v, position);
                    }
                });

            holder.llDefault.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.OnItem(v, position);
                }
            });
        }

        if ((!TextUtils.isEmpty(mList.get(position))) && mList.get(position).equals("default")) {
            holder.ivDelete.setVisibility(View.GONE);
            holder.ivImg.setVisibility(View.GONE);
            holder.llDefault.setVisibility(View.VISIBLE);
            holder.tvMaxNum.setText("(" + (getItemCount() - 1) + "/" + maxSelectNum + ")");
            return;
        } else holder.llDefault.setVisibility(View.GONE);

        holder.ivImg.setVisibility(View.VISIBLE);
        holder.ivDelete.setVisibility(openSelect ? View.VISIBLE : View.GONE);

        if (margin != 0) {
            ((RelativeLayout.LayoutParams) holder.ivImg.getLayoutParams()).rightMargin = margin;
            ((RelativeLayout.LayoutParams) holder.ivImg.getLayoutParams()).bottomMargin = margin;
            ((RelativeLayout.LayoutParams) holder.llDefault.getLayoutParams()).rightMargin = margin;
            ((RelativeLayout.LayoutParams) holder.llDefault.getLayoutParams()).bottomMargin = margin;
        }
        loadImg(holder.ivImg, getItem(position));
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.iv_img)
        ImageView ivImg;

        @BindView(R.id.iv_delete)
        ImageView ivDelete;

        @BindView(R.id.ll_default)
        LinearLayout llDefault;

        @BindView(R.id.tv_max_num)
        TextView tvMaxNum;


        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public void setImgHeight(int imgHeight) {
        this.imgHeight = imgHeight;
    }


    public void setMargin(int margin) {
        this.margin = margin;
    }

    public void setOpenSelect(boolean openSelect) {
        this.openSelect = openSelect;
    }

    public void setMaxSelectNum(int maxSelectNum) {
        this.maxSelectNum = maxSelectNum;
    }
}

